﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using shop.entity;
using shop.util;
using System.Data.SqlClient;
using System.Data;
namespace shop.dao
{
    public class RoleDao
    {
        public Role GetRole(int id){
            string sql = "select id, RoleName from role where id = @id";
            SqlParameter[] pms = { 
                                 new SqlParameter("@id", id),
                              
                                 };
            DataTable dt = SqlHelper.ExecuteDataTable(sql, pms);
           
            Role role  = new Role();
            if (dt.Rows.Count > 0) {
                EntityAdapt(role, dt.Rows[0]);
            }          
            return role;
        }
        public List<Role> ListRole()
        {
            string sql = "select id, roleName from role";
            DataTable dt = SqlHelper.ExecuteDataTable(sql, null);
            List<Role> roleList = new List<Role>();
            for (int i = 0; i < dt.Rows.Count;i++ )
            {
               Role role = new Role();
                EntityAdapt(role, dt.Rows[i]);
                roleList.Add(role);
            }

            return roleList;
        }
          private void EntityAdapt(Role role,DataRow row){
             role.Id = (int)row["id"];
             role.RoleName = (string)row["RoleName"];
          }
      
        public int AddRole(Role role) {
            string sql = "insert into role(roleName) values(@roleName) select @@identity  ";
            SqlParameter[] pms = { 
                                 new SqlParameter("@roleName", role.RoleName),                               
                                 };
            int count = SqlHelper.ExecuteNonQuery(sql,pms);           
                return count;         
        }

        public bool UpdateRole(Role role)
        {
            string sql = "update role set roleName = @roleName  where id =@id";
            SqlParameter[] pms = { new SqlParameter("@name", role.RoleName),
                         
                            new SqlParameter("@id", role.Id),
                              new SqlParameter("@roleName", role.RoleName)
                                 };
            int count = SqlHelper.ExecuteNonQuery(sql, pms);
            if (count > 0)
            {
                return true;
            }
            return false;
        }

        public bool DeleteRole(int id)
        {
            string sql = "delete from role where id = @id";
            SqlParameter[] pms = { new SqlParameter("@id", id) };
            int count = 0;
            try {
             count = SqlHelper.ExecuteNonQuery(sql, pms);
            }
            catch(Exception e) {
                Console.Write(e.Message);

            }
            if (count > 0)
            {
                return true;
            }
            return false;
        }
    }
}